﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Account.Model;
using Identity.IService.Entry;
using Framework.MVVM.Model;
using Common.IService;
using Identity.IService;
using Common.Service;
using Log.Service;
using Log.IService;
using Account.IService;
using Account.Service;
using Identity.Service;
using Account.Model.Behavior;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(IUserMailService), typeof(UserMailService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class NewUserMailModel : AsyncModelBase, IWriteLog, IValidateVerifyCode, IDeleteVerifyCode,IValidateIdentity, INewUserMail
    {
        #region Filed

        private const string m_verifycode_empty = "newmail00";
        private const string m_verifycode_error = "newmail01";
        private const string m_verifycode_complete = "newmail02";
        private const string m_verifyvalue_error = "newmail03";
        private const string m_verifyvalue_complete = "newmail04";
        private const string m_empty_identity = "newmail05";
        private const string m_error_identity = "newmail06";
        private const string m_complete_identity = "newmail07";
        private const string m_error_usermail = "newmail08";
        private const string m_complete_usermail = "newmail09";

        private const string m_resource = "Normal";
        private const string m_user_resource = "u000";
        private const char m_split = ';';

        private const string m_error = "error";
        private const string m_entry = "entry";

        private const string m_errors_verifycode = "verifycode";
        #endregion Filed

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            this.OnBeginRequest();
            this.ValidateVerifyCode(m_resource,m_verifycode_error, m_verifycode_complete);
            
        }

        #endregion AsyncModelBase

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            this.OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            this.OnEndRequest();
        }

        #endregion IWriteLog

        #region IValidateVerifyCode

        public void OnValidateVerifyCodeComplete(string key, bool entry)
        {
            this.DeleteVerifyCode(m_resource, m_verifycode_error, m_verifycode_complete);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, m_errors_verifycode);
            this.Log(key, new IdentityEntry { code = VerifyCode, source = Source, resource = m_resource, value = VerifyValue });
        }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        public bool IsOnce { get; set; }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            this.ValidateIdentity(m_user_resource, m_empty_identity, m_error_identity, m_complete_identity);
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            this.ValidateIdentity(m_user_resource, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion IDeleteVerifyCode

        #region IValidateIdentity

        public string IdentityCode { get; set; }
        public string Source { get; set; }

        public void OnValidateIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            UserMailCode = entry.value;
            this.NewUserMail(m_error_usermail, m_complete_usermail);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource });
        }

        #endregion IValidateIdentity

        #region INewUserMail

        public string UserMailAddress{ get; set; }

        public string UserMailCode{ get; set; }

        public void OnNewUserMailComplete(string key, Account.IService.Entry.UserMailEntry entry)
        {
            OnComplete(m_entry, entry);
            this.Log(key, Source + m_split + UserMailCode);
        }

        public void OnNewUserMailError(string key)
        {
            OnComplete(m_error, "mail");
            this.Log(key, Source + m_split + UserMailCode);
        }

        #endregion INewUserMail
    }
}